//
//  KeychainStore.swift
//  MintBag
//
//  Created by wy on 2024/8/23.
//

import Foundation
import SwiftyUserDefaults
import KeychainAccess

enum KeychainStoreKey: String {
    case sessionId
    case userId
    case userInfo
    case userPhone
    case firstOpen
    case uuid
    
    var key: DefaultsKey<String?> {
        return DefaultsKey<String?>(rawValue, defaultValue: "")
    }
}

class KeychainStore {
    public static let shared = KeychainStore()
    private let keychain = Keychain(service: "com.mbc.mintbag", accessGroup: "group.com.mbc.mintbag")
}

extension KeychainStore {
    func setKeychainString(_ value: String, key: KeychainStoreKey) {
        try? keychain.set(value, key: key.rawValue)
    }

    func keychainString(forKey key: KeychainStoreKey) -> String? {
        return try? keychain.getString(key.rawValue)
    }
    
    func setString(_ value: String, key: KeychainStoreKey) {
        Defaults[key: key.key] = value
    }

    func string(forKey key: KeychainStoreKey) -> String? {
        return Defaults[key: key.key]
    }

    func remove(forKey key: KeychainStoreKey) {
        Defaults[key: key.key] = nil
    }
}
